<?php
$need_install = true;
$need_connect = true;
$need_admin = true;
include('../inc/prepend.php');

if(isset($_POST['add_poll'])) {
	$p = $_POST;
	$errors = '';
	if(empty($p['title'])) {
		$errors .= 'Veuillez entrer un nom au sondage<br/>';
	}
	else {
		$id = conf('id');
		$id++;
		set_conf('id', $id);
		$sondage = $Sondages->add_sondage();
		$sondage->set_id($id);
		$sondage->set_open(0);
		$sondage->set_title($p['title']);
		$Sondages->save(DATA_DIR.'sondages.ds');
		$Configuration->save(DATA_DIR.'configuration.ds');
		$Sondage = new Sondage();
		$Sondage->set_votes(0);
		$Sondage->save(POLLS_DIR.$id.'.ds');
		redirect('poll.php?id='.$id);
	}
}

$titre_page = 'Ajouter un sondage';
?>
<h2>Ajouter un sondage</h2>
<form method="post" action="add_poll.php">
	<?php
	if(!empty($errors)) {
		echo '<p style="color: red;">Erreurs :<br/>'.$errors.'Réessayez !</p>';
	}
	?>
	<label for="title">Question du sondage</label>
	<input type="text" name="title"/><br/>
	<input type="submit" name="add_poll" value="Enregistrer" />
</form>
<?php
include(INC_DIR.'template.php');
?>